﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HustQQInterface;
using HustQQClient.MsgDispatcher;

namespace HustQQClient.UI
{
    public partial class SingleChat : Form
    {
        string nickname;
        string friendId;
        string note;

        public SingleChat(string name, string id, string note, Image img)
        {
            InitializeComponent();
            nickname = name;
            friendId = id;
            this.note = note;
            picFace.Image = img;
            labelName.Text = nickname + "(" + id + ")";
            labelNote.Text = note;
            progressBar1.Maximum = 100;
            this.FormClosed += new FormClosedEventHandler(SingleChat_FormClosed);
            InterfaceManager.NetManager.ConfirmSendFile += new BooleanMsg(NetManager_ConfirmSendFile);
            InterfaceManager.NetManager.SendFileOver += new Action(NetManager_SendFileOver);
            InterfaceManager.NetManager.ReceiveFileOver += new FileMsg(NetManager_ReceiveFileOver);
            InterfaceManager.NetManager.RecievePicFromSingle += new SinglePicMsg(NetManager_RecievePicFromSingle);
            (InterfaceManager.NetManager as NetImplement).Client.Port.RecieveFileRate += new ReturnVal(Port_RecieveFileRate);
            (InterfaceManager.NetManager as NetImplement).Client.Port.SendFileRate += new ReturnVal(Port_SendFileRate);
        }

        void SingleChat_FormClosed(object sender, FormClosedEventArgs e)
        {
            InfoControl.dictSingleChat.Remove(friendId);
        }

        #region 接口实现
        void Port_SendFileRate(double rate)
        {
            progressBar1.Invoke(new Action(() =>
            {
                progressBar1.Value = (int)(100 * rate);
            }));
        }

        void Port_RecieveFileRate(double rate)
        {
            progressBar1.Invoke(new Action(() =>
            {
                progressBar1.Value = (int)(100 * rate);
            }));
        }

        void NetManager_RecievePicFromSingle(string userId, string picPath, DateTime time)
        {
            throw new NotImplementedException();
        }

        void NetManager_ReceiveFileOver(string userId, HustQQModel.FileInformation fileInfo, DateTime time)
        {
            this.Invoke(new Action(() =>
                {
                    labelInfo.Text = ("接收文件： " + fileInfo.FileName + " 完毕！");
                    progressBar1.Value = 0;
                }));
        }

        void NetManager_SendFileOver()
        {
            this.Invoke(new Action(() =>
                {
                    labelInfo.Text = "发送文件完毕!";
                    progressBar1.Value = 0;
                }));
        }

        void NetManager_ConfirmSendFile(bool isOk)
        {
            this.Invoke(new Action(() =>
                {
                    if (isOk)
                        labelInfo.Text = "对方同意接受您的文件";
                    else
                        labelInfo.Text = "对方拒绝接受您的文件";
                }));
        }
        #endregion

        public void DisplayTextInfo(string name, string text, string time)
        {   
            textRecv.Text += name + " " + time + "\r\n";
            textRecv.Text += "    " + text;
            textRecv.Text += "\r\n\r\n";
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            InterfaceManager.NetManager.SendTextToSingle
                (InfoControl.CurrentUser.qq_ID, friendId, textSend.Text);
            DisplayTextInfo(InfoControl.CurrentUser.name, textSend.Text, DateTime.Now.ToString());
            textSend.Text = "";
        }

        private void btnSendFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filePath = openFileDialog1.FileName;
                InterfaceManager.NetManager.SendFile(InfoControl.CurrentUser.qq_ID, friendId, filePath);
                labelInfo.Text = "正在等待对方响应......";
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSendPic_Click(object sender, EventArgs e)
        {
            //OpenFileDialog ofd = new OpenFileDialog();
            //ofd.Filter = "bmp File(*.bmp)|*.bmp;jpg FILE(*.jpg)|*.jpg;png FILE(*.png)|*.png";
            //if (ofd.ShowDialog() == DialogResult.OK)
            //{
            //    string filePath = openFileDialog1.FileName;
            //    InterfaceManager.NetManager.SendPictureToSingle(InfoControl.CurrentUser.qq_ID,
            //        nickname, filePath);
            //}
            PictureBox pb = new PictureBox();
            pb.Image = new Bitmap(@"D:\team.bmp");
            textRecv.Controls.Add(pb);
            pb.Location = new Point(50, 50);
            //textRecv.Text += "fdsfdsfd\r\ndsfdfds\r\n";

            //Image img = Image.FromFile(@"D:\team.bmp");
            //Clipboard.SetDataObject(img);
            //textRecv.ReadOnly = false;
            //textRecv.Paste(DataFormats.GetFormat(DataFormats.Bitmap));
            //textRecv.ReadOnly = true;
            //textRecv.Text += "fdsfsdfsfdsfdsfdf";
        }

    }
}
